﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.CallRoute;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule;

public partial class masterdata_Customer_View : PageBase
{
    #region 定义变量
    private decimal id = 0;
    private const string CUSTOMER_ID = "id";
    private const string RETURN_PAGE = "Customer_List_View.aspx";

    private OrgainzationManager orgManager = new OrgainzationManager();

    #endregion

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        decimal.TryParse(Request.QueryString[CUSTOMER_ID], out id);

        if (!IsPostBack)
        {
            ScriptManager ScriptManager1 = Page.Master.FindControl("ScriptManager1") as ScriptManager;
            ScriptManager1.Services.Add(new ServiceReference("~/GIS/GService.asmx"));
            ScriptManager1.Services.Add(new ServiceReference("~/GIS/GoogleMapForASPNet.ascx"));

            DataBind();
            if (Request.QueryString[CUSTOMER_ID] != null)
            {

                btnNext_Click(sender, e);


                
            }
            InitForm();
            this.FillForm(sender, e);
        }
    }

    #region 下拉框数据绑定
    /// <summary>
    /// Inits the form.
    /// </summary>
    private void InitForm()
    {
        DictionaryItemsManager dim = new DictionaryItemsManager();

        //销售渠道
        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();

        //销量等级
        ddlStore_Level.DataTextField = "Name";
        ddlStore_Level.DataValueField = "DictionaryItemId";
        ddlStore_Level.DataSource = dim.GetItemList(DictionaryEnum.Store_Level, this.MyStateManager.DomainID);
        ddlStore_Level.DataBind();

        //ddlMeasures.DataSource = new MeasureProfilesManager().GetMeasuresProfilesDistributionTable(MyStateManager.DomainID, "1");
        //ddlMeasures.DataTextField = "NAME";
        //ddlMeasures.DataValueField = "ProfilesID";
        //ddlMeasures.DataBind();
        ddlMeasures.DataSource = new MeasuresGroupManager().GetMeasuresGroupTable(MyStateManager.DomainID, "1");
        ddlMeasures.DataTextField = "NAME";
        ddlMeasures.DataValueField = "ID";
        ddlMeasures.DataBind();
        ddlMeasures.Items.Insert(0, new ListItem(Resources.language.ddl_Select, ""));

       
        //客户类型
        drpCustomerType.DataTextField = "Name";
        drpCustomerType.DataValueField = "DictionaryItemId";
        drpCustomerType.DataSource = dim.GetItemList(DictionaryEnum.CustomerClass, this.MyStateManager.DomainID);
        drpCustomerType.DataBind();
        drpCustomerType.Items.Insert(0, new ListItem(Resources.language.ddl_Select, ""));
    


    }
    #endregion

    /// <summary>
    /// Fills the form.
    /// </summary>
    private void FillForm(object sender, EventArgs e)
    {
        if (id > 0)
        {
            CUSTOMERS customer = new CustomerManager().Load(id);
            if (customer != null)
            {
                //查找业务代表
                DataTable dt = new CustomerRouteManager().GetUsersByCustomer(customer.ID);
                if (dt.Rows.Count > 0)
                {
                    txtMobileUser.Value = dt.Rows[0]["NAME"].ToString();
                }
                txtName.Text = customer.NAME;
                chxValid.Checked = customer.ACTIVE;

                if (customer.ORGID != null)
                    txtOrg.Text = orgManager.Load(customer.ORGID).NAME;

                trCustomerID.Visible = true;
                txtCustomerID.InnerText = customer.CODE;

                //获取经度，纬度,以后需要修改，店面位置，门店面积用的也是IDENTITY1,IDENTITY2字段
                txtLongitude.Text = customer.IDENTITY1.ToString();
                txtlatitude.Text = customer.IDENTITY2.ToString();

                drpCustomerType.SelectedValue = customer.CUSTCLASSCODE;
                drpCustomerType.Enabled = false;

                ddlChannel.SelectedValue = customer.LOCALLEVEL1CODE;
                try
                {
                    foreach (ListItem item in ddlMeasures.Items)
                    {
                        if (item.Value == customer.MEASUREPROFILEID.ToString())
                        {
                            item.Selected = true;
                            break;
                        }
                    }

                }
                catch { }
                ddlMeasures.Enabled = false;
                try
                {
                    foreach (ListItem item in ddlStore_Level.Items)
                    {
                        if (item.Value == customer.BRACKETID.ToString())
                        {
                            item.Selected = true;
                            break;
                        }
                    }

                }
                catch { }
                ddlStore_Level.Enabled = false;
            }
            var contact = new ContactManager().Load(id);
            if (contact != null)
            {
                txtAddress.Text = contact.ADDRESSLINE;
                txtPhone.Text = contact.TELEPHONE;
                txtZipCode.Text = contact.POSTALCODE;
                txtContact.Text = contact.SLOGCODE;
                ///通过最小一级获取省市区
                try
                {

                    int level = 0;
                    String Code = "";
                    if (!string.IsNullOrEmpty(contact.DISTRICT))
                    {
                        level = 4;
                        Code = contact.DISTRICT;
                    }
                    else if (!string.IsNullOrEmpty(contact.CITY))
                    {
                        level = 3;
                        Code = contact.CITY;
                    }
                    else if (!string.IsNullOrEmpty(contact.STATE))
                    {
                        level = 2;
                        Code = contact.STATE;
                    }
                    else if (!string.IsNullOrEmpty(contact.COUNTRYCODE))
                    {
                        level = 1;
                        Code = contact.COUNTRYCODE;
                    }
                    else
                    {
                        level = 0;

                    }
                    var dt = new RegionManager().GetRegionData(MyStateManager.DomainID, level, Code);
                    var region = dt[0] as REGIONS;
                    if (level == 4)
                    {
                        this.txtProvince.Text = region.STATENAME;
                        this.txtViewCity.Text = region.CITYNAME;
                        this.txtArea.Text = region.DISTRICTNAME;

                    }
                    else if (level == 3)
                    {
                        this.txtProvince.Text = region.STATENAME;
                        this.txtViewCity.Text = region.CITYNAME;
                    }
                    else if (level == 2)
                    {
                        this.txtProvince.Text = region.STATENAME;
                    }


                }
                catch
                {

                }
            }
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["target"]))
            Response.Redirect(Request.QueryString["target"]);
        Response.Redirect(RETURN_PAGE + "?" + Request.QueryString);
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (id > 0)
        {
            if (ViewState["pageIndex"] == null)
            {
                ViewState["pageIndex"] = 1;
            }
            else
            {
                ViewState["pageIndex"] = int.Parse(ViewState["pageIndex"].ToString()) + 1;
            }
           
        }

    }

   
}

